/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.core.util.RandomUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.modules.auth.utils.WxMaConfiguration;
import org.springblade.modules.auth.utils.WxMaProperties;
import org.springblade.modules.repair.entity.MaintainTemplateHistory;
import org.springblade.modules.repair.vo.MaintainTemplateHistoryVO;
import org.springblade.modules.repair.mapper.MaintainTemplateHistoryMapper;
import org.springblade.modules.repair.service.IMaintainTemplateHistoryService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * 保养模板历史表 服务实现类
 *
 * @author lsay
 * @since 2023-12-14
 */
@Service
public class MaintainTemplateHistoryServiceImpl extends BaseServiceImpl<MaintainTemplateHistoryMapper, MaintainTemplateHistory> implements IMaintainTemplateHistoryService {

	@Autowired
	private WxMaProperties wxMaProperties;
	@Autowired
	private OssBuilder ossBuilder;

	@Override
	public IPage<MaintainTemplateHistoryVO> selectMaintainTemplateHistoryPage(IPage<MaintainTemplateHistoryVO> page, MaintainTemplateHistoryVO maintainTemplateHistory) {
		return page.setRecords(baseMapper.selectMaintainTemplateHistoryPage(page, maintainTemplateHistory));
	}

	@Override
	public String generateWxAppCode(Long templateId) {
		final WxMaService wxService = WxMaConfiguration.getMaService(wxMaProperties.getConfigs().get(0).getAppid());
		byte[] codeByte = null;
		try {
			// 生成正式版小程序码
			codeByte = wxService.getQrcodeService().createWxaCodeUnlimitBytes(String.valueOf(templateId),"subpages/workOrders/detailPreview",  430, true, null,true);

			// 生成体验版小程序码
//			WxCodeUnlimitExt wxCodeUnlimitExt = new WxCodeUnlimitExt();
//			wxCodeUnlimitExt.setScene(String.valueOf(elevatorId));
//			wxCodeUnlimitExt.setPage("pages/elevatorList/elevatorDetail");
//			codeByte = wxService.execute(QrcodeBytesRequestExecutor.create(wxService.getRequestHttp()), GET_WXACODE_UNLIMIT_URL, wxCodeUnlimitExt);
		} catch (WxErrorException e) {
			throw new ServiceException("获取微信小程序码失败！");
		}

		InputStream inputStream = new ByteArrayInputStream(codeByte);
		String fileName = RandomUtil.randomString(8) + ".png";
		BladeFile bladeFile = ossBuilder.template().putFile(fileName, inputStream);
		return bladeFile.getLink();
	}

}
